<script language= "javascript">
/**
 * 清除维度下拉列表框内容
 * @oSelect 列表对象
 */
function clearSelect(oSelect){
	var oOptions = oSelect.options;
        var longths = oOptions.length;
	for(i=0;i<=longths;i++){
		oOptions.remove(0);
	}
}

function removeHiddenField( oForm, oFieldName ) {
	var cols = new Array();
	var count = 0;
	
	var oChildren = oForm.getElementsByTagName( "input" );
	
	// 查找隐藏域
	for(var i = 0; i < oChildren.length; i++)
	{
	        if( oChildren[i].name == oFieldName )
	        {
			cols[count] = oChildren.item(i);
			count++;
		}
	}
		
	for(var i = 0; i < cols.length; i++)
	{
		oForm.removeChild( cols[i] );
	}
}

function toggle( id, showName, hideName ) 
{
    if( showName == null 
    	|| showName.length == 0 )
    	showName = "显示";
    
    if( hideName == null 
    	|| hideName.length == 0 )
    	hideName = "隐藏";
    	
    var element = document.getElementById(id);
    with (element.style) {
        if ( display == "none" ){
            display = ""
        } else{
            display = "none"
        }
    }
    var text = document.getElementById(id + "-switch").firstChild;
    if (text.nodeValue == showName) {
        text.nodeValue = hideName;
    } else {
        text.nodeValue = showName;
    }
}
var additionalPart = ""; // 打开任务时间选择页面，其他查询字符
var unitIDselected = new Array();
var timeID = new Array(); // 任务时间id数组
var timeDesc = new Array(); // 任务时间描述数组
var oPopWin;

<% tasktimes = Yttasktime.find(:all, :conditions=>"taskid= #{session[:task].id}")%>

<% Integer(0).upto(tasktimes.size-1) do |index|%>
    timeID[<%=index%>] = "<%=tasktimes[index].id%>";    
    timeDesc[<%=index%>] = "<%=GetTaskTimeDescString(tasktimes[index])%>";
<% end%>

</script>

<script>

function afterSelectUnitIDs()
{
	unitIDselected = new Array();
	
	var opopUnitIDs = oPopWin.unitIDselected;
	for( var i=0; i<opopUnitIDs.length; i++ )
	{
		unitIDselected[i] = opopUnitIDs[i];
	}
	
	var unitNames = new Array();
	var opopUnitNames = oPopWin.unitNameselected;
	for( var i=0; i<opopUnitNames.length; i++ )
	{
		unitNames[i] = opopUnitNames[i];
	}
	
	fillUnitSelect( unitNames );
}

function fillUnitSelect( unitNames ) 
{
	// 清除原来记录，并且显示select区域
	var unitSelect = document.getElementById( "unitSelect" );
	if (!unitSelect)
	{
	   return;
	}
	clearSelect( unitSelect ); 
	unitSelect.style.display = "inline";
	
	var size = 0;
	for( var i=0; i<unitNames.length; i++ )
	{
		var s = unitNames[i];
		
		if( s != null
			&& s.length > 0 )
		{
			var oOption = document.createElement( "option" );
			oOption.innerText = " " + s + " ";
			unitSelect.appendChild( oOption );
			
			size++;
		}
	}
	
	if( size == 1 )
	{
		unitSelect.size = size;
		unitSelect.selectedIndex = 0;
	}
	else if( size < 10 )
	{
		unitSelect.size = size;
	}
	else
	{
		unitSelect.size = 10;
	}
}

function selectTaskTimes()
{
	var url = "/templatequery/custom_tasktimes/";
	window.open( url, null, "location=no,menubar=no,toolbar=no,height=300,width=500" );
	
	additionalPart = ""; // 值复位
} 

function afterSelectTaskTimes()
{
	fillTimeSelect();
}

function fillTimeSelect()
{
	// 清除原来记录，并且显示select区域
	var timeSelect = document.getElementById( "timeSelect" );
	clearSelect( timeSelect ); 
	
	var oTimes = execTplForm.taskTimeIDs.value;
	var beginIndex = 0;
	var endIndex= oTimes.indexOf( ",", beginIndex );
	
	var size = 0;
	while( endIndex > 0 )
	{
		var s = oTimes.substring( beginIndex, endIndex );
		var oOption = document.createElement( "option" );
		oOption.innerText = " " + getTimeDesc( s ) + " ";
		timeSelect.appendChild( oOption );
		beginIndex = endIndex+1;
		
		endIndex = oTimes.indexOf( ",", beginIndex );
		
		size++;
	}
	
	if( size == 1 )
	{
		timeSelect.size = size;
		timeSelect.selectedIndex  = 0;
	}
	else if( size < 5 )
	{
		timeSelect.size = size;
	}
	else
	{
		timeSelect.size = 5;
	}
}


function getTimeDesc( tid )
{
	for( var i=0; i<timeID.length; i++ )
	{
		if( timeID[i] == tid ) 
		{
			return timeDesc[i];
		}
	}
}


function execTpl(index)
{
	execTplForm.tplIDs.value = index;
	
	var oTaskTimeIDs = document.getElementById( "taskTimeIDs" );
	
	if( oTaskTimeIDs.value.length == 0 )
	{
		alert( "请选择任务时间" );
		return false;
	}
	
	
	var result = "";
	for( var i=0; i<unitIDselected.length; i++ )
	{
	    
		if( unitIDselected[i] != null 
			&& unitIDselected[i].length > 0 && unitIDselected[i] != '<<没有单位>>' )
		{
		    result += ","  +  unitIDselected[i];
		}		
	}
	
	if (execTplForm.unitschema.value!='-1')
	{
	   result = execTplForm.unitschema.value;
	}
	
	if (result == "")
	{
		alert('请选择单位');
		return;
	}
	execTplForm.unitIDs.value = result.substr(0, result.length-1);
	execTplForm.templateID.value = index;
	
	execTplForm.submit();
}

function showUnitTree()
{
	oPopWin = window.open( "/unitmanager/selectunit?doAfterDone=window.opener.afterSelectUnitIDs();", 
					null, 
					"location=no,menubar=no,toolbar=no,height=500,width=800,resizable=yes" );
}

function enableDefaultUnits()
{
	unitIDselected = new Array();
		
	var unitSelect = document.getElementById("unitSelect");
	//unitSelect.style.display = "none";
	
	var unitNames = new Array();
	unitNames[0] = "<<没有单位>>";
	fillUnitSelect( unitNames );
}

function enableCustomeUnits()
{
	//unitIDselected = new Array();
	showUnitTree();
}

function enableSuitTime()
{
	execTplForm.taskTimeIDs.value="<%=session[:tasktime]%>" + ",";
	
	fillTimeSelect();
}

function enableCustomTime()
{	
	selectTaskTimes();
}

function up( index )
{
	var diff = 3;
	var oTable = document.getElementById( "tplTable" );
	
	var oUpwardRow = oTable.rows( index+diff );
	var oDownwardRow = oTable.rows( index+diff-1 );
	
	changeRow( oUpwardRow, oDownwardRow );
}

function down( index )
{
	var diff = 3;
	var oTable = document.getElementById( "tplTable" );
	
	var oUpwardRow = oTable.rows( index+diff+1 );
	var oDownwardRow = oTable.rows( index+diff );
	
	changeRow( oUpwardRow, oDownwardRow );
}

function changeRow( oRow1, oRow2 )
{
	// 只改变第一个checkbox和第二个文字标签
	var oTemp = oRow1.cells(0).children(0).name;
	oRow1.cells(0).children(0).name = oRow2.cells(0).children(0).name;
	oRow2.cells(0).children(0).name = oTemp;
	
	oTemp = oRow1.cells(0).children(0).checked;
	oRow1.cells(0).children(0).checked = oRow2.cells(0).children(0).checked;
	oRow2.cells(0).children(0).checked = oTemp;
	
	oTemp = oRow1.cells(1).innerText;
	oRow1.cells(1).innerText = oRow2.cells(1).innerText;
	oRow2.cells(1).innerText = oTemp;
}

function afterLoad()
{
	//enableSuitTime();
	enableDefaultUnits();
}

afterLoad();

</script>
<div onload="afterLoad()" id='topnode' style="display:none">
<script>
Effect.Appear('topnode');
</script>

<table width = "100%" >


<tr width="100%">

<form id="execTplForm" action="/templatequery/query" method="post"  target="_blank">
	
	<input type="hidden" name="operation" value="executeScalarQueryTemplate"/>
	<input type="hidden" name="tplIDs" />
	<input type="hidden" name="unitIDs" />
	<input type="hidden" name="templateID" />
	<input type="hidden" name="taskTimeIDs" id="taskTimeIDs" value="<%=session[:tasktime]%>," />

<td align="left" valign="top" width="60%">	
<p>&nbsp;
	<table width="100%" valign="top">
		<tr>
			<td colspan="4">
				<a id="ttime-switch" href="javascript:toggle('ttime', '时间选项', '时间选项')" >时间选项</a>
				&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
				<%= TaskTimeCombo(session[:task].id, "changeTaskTime(this)")%>
                <script>
                changeTaskTime(execTplForm.tasktime);
				function changeTaskTime(obj){
                    execTplForm.taskTimeIDs.value = obj.value;
                }
</script>
			</td>
		</tr>
	
		<tr>
			<td colspan="4">
				<hr size="1" color="green"/>
			</td>
		</tr>
	
		<tr id="ttime" style="display:none">
			
			
			<td valign="top" width="30%" colspan=2>
				<a href="javascript:enableCustomTime()">自定义时间</a>
			</td>
			
			<td valign="top" width="60%">					
				<select id="timeSelect" size="5">
				</select>
			</td>
		</tr>
	
		
		<tr>
			<td colspan="4">&nbsp;</td>
		</tr>
		
		<tr>
			<td colspan="4">
				<a id="uunit-switch" href="javascript:toggle('uunit', '单位选项', '单位选项')" >单位选项</a>
				&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
				<select name="unitschema">
			     <% schemas = YtaplUnitschema.find(:all, :conditions=>"taskid=#{session[:task].id}") %>
			     <%=options_from_collection_for_select(schemas, "content", "name")%>
			     <option value="0">所有有权限的单位</option>
			     <option value="-1">自定义</option>
			     </select>
			</td>
		</tr>
		
		<tr>
			<td colspan="4">
				<hr size="1" color="green"/>
			</td>
		</tr>
		
		<tr id="uunit" style="display:none">
			<td valign="top" width="30%" colspan=2>
				<a href="javascript:enableCustomeUnits()">自定义单位</a>
			</td>
			
			<td valign="top" width="60%">					
				<select id="unitSelect" name="unitSelect" size="10" style="display: none">
				</select>
			</td>
		</tr>
	
		<tr>
			<td colspan="4">&nbsp;</td>
		</tr>
		
		<tr>
		  <td>
		  金额单位：&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
		  <select name="currency">
                <%=options_for_select([["元",'1'], ["百元", "100"], ["千元","1000"], ["万元","10000"], ["千万","10000000"], ["亿元","100000000"]], "10000")%>
          </select>
          </td>
		</tr>
		
		<tr>
			<td colspan="4">
				<hr size="1" color="green"/>
			</td>
		</tr>
		
		<tr>
		  <td>
		  <input type="checkbox" name="toexcel" value="true" />导出到Excel&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
		  <input type="checkbox" name="savedata" value="true" />保存数据（以备下次查询）&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
          </td>
		</tr>
		
		<tr>
			<td colspan="4">
				<hr size="1" color="green"/>
			</td>
		</tr>
		
        <tr id="print">
			<td width="100%" colspan=2>所有模板</td>
		</tr>
	
		<tr>
		
		  <td colspan="4">
		      <table>
		      <%for template in @templates%>
		          <tr>
		              <td>
		                  <input type="checkbox" name="templates[]" value="<%=template.id%>" style="margin:0px 0px"> 
		                  <%=template.templatename%>
		              </td>
		          </tr>
		      <%end%>
		      <tr><td><input type="button" onclick="execTpl()" value="查询"></td></tr>
		      </table>
		  </td>
		</tr>
		
	</table>
</td>
</form>
<td valign="top" width="45%">
   <%= form_remote_tag :update => "queryresult", :url => {:action=> 'switchcatalog'}, :html=>{:name=>'resultform', :id=>'resultform'} %>
    历史查询结果:
    <select name="catalog" onchange="this.form.onsubmit()">
        <%= options_from_collection_for_select(@templates, "templateid", "templatename")%>
    </select>
   <%= end_form_tag %>
   
   <div id="queryresult" width = "100%">
        <% if @templates.size > 0%>
            <%= render :partial=>'result', :locals => { :template => @templates[0] } %>
        <% else%>
            <%= render :partial=>'result', :locals => { :template => nil} %>
        <% end%>
   </div> 
</td>
</tr>
	
</table>	

</div>
